import axios from 'axios'
import qs from 'qs'
import {Message} from 'element-ui'
import router from '../main'

let baseURL = ''
if(process.env.NODE_ENV == 'development'){
    //测试环境
     // baseURL = "http://192.168.0.152:8181/AdminAPI" //刘洋
    //  baseURL = "http://192.168.0.127:8050/AdminAPI" //鹏飞
     baseURL = "http://192.168.0.128:8181/AdminAPI" //志伟
}else if(process.env.NODE_ENV == 'debug'){

}else if(process.env.NODE_ENV == 'production'){
    //线上环境
    baseURL = ''
}
console.log('process', process)
export const Axios = axios.create({

  baseURL: `${baseURL}`,
  timeout: 20000,
  transformRequest: [
    function(data) {
      // 对 data 进行任意转换处理
      data = JSON.stringify(data);
      
      return data;
    }
  ]
});

//请求拦截器
Axios.interceptors.request.use(
  config => {
    config.headers['Content-Type'] = 'application/json'
    if (sessionStorage.getItem('token')) {
      config.headers.token = sessionStorage.getItem('token');
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 返回状态判断(添加响应拦截器)
Axios.interceptors.response.use(
  res => {
    //对响应数据处理

    return res;
  },
  error => {
    //返回错误后续做统一处理
    return Promise.resolve(error);
  }
);

const creatError = (code, msg) => {
  const err = new Error(msg);
  err.code = code;
  err.msg = msg;
  return err;
};

const handleRequest = request => {
  return new Promise((rsolve, reject) => {
    request
      .then(res => {
        const data = res.data;
        let err = null;
        if (!data) {
          return reject(creatError(400, "服务器出错或链接超时请重试"));
        }
        if (data.code != 200) {
          // 通用 200 请求成功
          switch (true) {
            case data.code == 401 || data.code == 402:
           
              err = creatError(data.code, "未登录或登录已过期请重新登录");
              sessionStorage.clear();
              
              router.push("/index");
             // window.location.reload()
              break;
            default:
              err = creatError(data.code, data.message);
              break;
          }
          return reject(err);
        }
        // console.log(router)
        return rsolve(data);
      })
      .catch(e => {
        return reject(e);
      });
  });
};


  export function post(url, param, calbak,trueparam) {
    
    return handleRequest(
      Axios({
        url: url,
        method: "post",
        data: param,
        // data: qs.stringify(param), form-data传递
        params: trueparam
      })
    )
      .then(res => calbak(res))
      .catch(e => {
        Message.error(e.message);
      });
  }
  // 不使用
  export function postForm(url, param, calbak) {
    
    return handleRequest(
      axios({
        url: `${baseURL}` + url,
        method: "post",
        data: qs.stringify(param), //form-data传递
      })
    )
      .then(res => calbak(res))
      .catch(e => {
        Message.error(e.message);
      });
  }
  
  export function get(url, param, calbak) {
    return handleRequest(
      Axios({
        url: url,
        method: "get",
        params: param
      })
    )
      .then(res => calbak(res))
      .catch(e => {
        Message.error(e.message);
      });
  }